package com.goldcn.sys.service;

import com.goldcn.common.ResultData;
import com.goldcn.common.util.IdGen;
import com.goldcn.sys.dao.SysAppMapper;
import com.goldcn.sys.dao.SysConfigMapper;
import com.goldcn.sys.model.SysApp;
import com.goldcn.sys.model.SysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Tim Peng
 * mail: lmn_022@163.com
 * Date: 2017/8/24
 * Time: 10:30
 * To change this template use File | Settings | File Templates.
 */
@Service
@Transactional
public class SysAppService {

    @Autowired
    SysAppMapper sysAppMapper;

    @Autowired
    SysConfigMapper sysConfigMapper;

    public Object getAppInfo(Long companyID) {


        List<Map<String, Object>> appInfos = sysAppMapper.getMapByCompanyID(companyID);

        Map<String, Object> sysConfig = sysConfigMapper.getMapByPrimaryKey(companyID);

        Map<String, Object> map = new HashMap();

        map.put("sysConfig", sysConfig);
        map.put("appInfos", appInfos);


        return map;
    }

    public Object updateAppInfo(SysApp sysApp) {
        SysConfig sysConfig = sysConfigMapper.selectByPrimaryKey(sysApp.getCompanyID());
        if (null == sysConfig) {
            ResultData resultData = new ResultData();
            resultData.setCode("125");
            return resultData;
        }

        if (null == sysApp.getAppID()) {
            sysApp.setCompanyID(sysConfig.getCompanyInfoID());
            sysApp.setAppID(IdGen.get().nextId());
            sysAppMapper.insert(sysApp);
        } else {
            sysAppMapper.updateByPrimaryKeySelective(sysApp);
        }


        return sysAppMapper.getMapByPrimaryKey(sysApp.getAppID());
    }

    public Object deleteAppInfo(Long appID) {
        SysApp sysApp = sysAppMapper.selectByPrimaryKey(appID);
        sysApp.setFileAddress(null);
        sysAppMapper.updateByPrimaryKey(sysApp);
        return new ResultData();
    }
}
